var utils = require('../../../util/utils');
var model = require('../../../model/model');
var logger = require('pomelo-logger').getLogger('connector',__filename);  //日志文件
module.exports = function(app)
{
    return new Handler(app);
};

var Handler = function(app)
{
    this.app = app;
    this.channelService = app.get('channelService');
};

var handler = Handler.prototype;
var channelName = 'player';

handler.enter = function(msg, session, next)
{
	var self = this;
    var client_info =
    {
        'playerId'  :msg.playerId,
        'imei'      :msg.deviceId,
        'imsi'      :msg.imsi,
        'gameId'    :msg.gameId,
        'gameVer'   :msg.gameVer,
        'os'        :msg.os,
        'model'     :msg.model
    };
    var createSession = function(player)
    {
        console.info("createSession");
        var playerId = player.playerId;
        session.set('name',player.nickName);
        session.bind(playerId);
        session.pushAll();
        session.on('closed',onPlayerLeave.bind(null,self.app));
        var serverId = self.app.get('serverId');
        self.app.rpc.player.playerRemote.login
        (
            session,
            playerId,
            serverId,
            channelName,
            function()
            {
                next(null,player);
            }
        );
    }
    if( 'win32' == msg.os )
    {
        console.log('client os:windows');
        console.log('client playerId:',msg.playerId);
        model.getPlayerInfo(msg.playerId,function(err,player)
        {
            if( err )
            {
                console.info("getPlayerInfo error");
                console.log(err);
            }
            else
            {
                if( player )
                {
                    createSession(player);
                }
                else
                {
                    console.info('create desktop client player');
                    model.createPlayer(client_info,function(err,newPlayer)
                    {
                        createSession(newPlayer);
                    });
                }
            }
        });
    }
    else
    {
        console.info('client imei:',client_info.imei);
		model.getPlayerInfoByImei(client_info.imei,function(err,player)
        {
            if( err )
            {
                console.error(err);
            }
            else
            {
                if( player )
                {
                    createSession(player);
                }
                else
                {
                    client_info.playerId = null;
                    model.createPlayer(client_info,function(err,res)
                    {
                        if(err)
                        {
                            console.log(err);
                        }
                        else
                        {
                            createSession(res);
                        }
                    });
                }
            }
        });
	}
};
var onPlayerLeave = function(app, session)
{
    if( !session || !session.uid )
    {
        return;
    }
    var uid = session.uid;
    var sid = app.get('serverId');
    var me = session.get('me');
    var table = session.get('table');

    app.rpc.player.playerRemote.kick(session,uid,sid,channelName,null);

    if( me )
    {
        app.rpc.fight.fightRemote.fightLeave(session,me,table,function()
        {

        });
    }
    if( session.get('inFight') )
    {
        app.rpc.fight.fightRemote.fightEscape(session,me,table,function()
        {

        });
    }
};